package com.common.app.widget

import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.animation.DecelerateInterpolator

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：12/6/2025
 *
 * 描述：带动画效果的圆形进度条
 *
 * 修订历史：
 *
 */
class AnimatedRoundProgressBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : RoundProgressBar(context, attrs, defStyleAttr) {

    private var animator: ValueAnimator? = null
    var animationDuration: Long = 1500
    private var animatedProgress: Float = 0f

    /**
     * 设置进度并播放动画
     * @param progress 目标进度值 (0-100)
     */
    fun setProgressWithAnimation(progress: Int) {
        // 取消正在进行的动画
        cancelAnimation()

        // 创建从当前进度到目标进度的动画
        animator = ValueAnimator.ofFloat(animatedProgress, progress.toFloat()).apply {
            duration = animationDuration
            interpolator = DecelerateInterpolator() // 使用减速插值器，效果更自然
            addUpdateListener { animation ->
                animatedProgress = animation.animatedValue as Float
                super.setProgress(animatedProgress.toInt())
            }
            addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationStart(animation: android.animation.Animator) {
                    isEnabled = false // 动画开始时禁用交互
                }

                override fun onAnimationEnd(animation: android.animation.Animator) {
                    isEnabled = true // 动画结束时启用交互
                }
            })
            start()
        }
    }

    /**
     * 设置进度并从指定的起始值播放动画
     * @param oldProgress 起始进度值 (0-100)
     * @param newProgress 目标进度值 (0-100)
     */
    fun setProgressWithAnimation(oldProgress: Int, newProgress: Int) {
        // 取消正在进行的动画
        cancelAnimation()

        // 设置起始值
        animatedProgress = oldProgress.toFloat()
        super.setProgress(oldProgress)

        // 创建从起始进度到目标进度的动画
        animator = ValueAnimator.ofFloat(oldProgress.toFloat(), newProgress.toFloat()).apply {
            duration = animationDuration
            interpolator = DecelerateInterpolator()
            addUpdateListener { animation ->
                animatedProgress = animation.animatedValue as Float
                super.setProgress(animatedProgress.toInt())
            }
            start()
        }
    }

    /**
     * 取消正在进行的动画
     */
    fun cancelAnimation() {
        animator?.cancel()
        animator = null
    }

    /**
     * 重置动画和进度
     */
    fun resetAnimation() {
        cancelAnimation()
        animatedProgress = 0f
        super.setProgress(0)
    }

    /**
     * 重写父类的setProgress方法，更新animatedProgress值
     */
    override fun setProgress(progress: Int) {
        animatedProgress = progress.toFloat()
        super.setProgress(progress)
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        cancelAnimation() // 确保在View被移除时取消动画，防止内存泄漏
    }
}